package com.hlyj.http.base.tool.lib_hlyj_base.utils

import android.text.TextUtils
import android.util.Log
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import org.json.JSONException
import org.json.JSONObject
import java.lang.reflect.Type
import java.util.*
import kotlin.collections.HashMap


/**
 * Created by 1 on 2019/11/11.
 */

/**
 * 封装的是使用Gson解析json的方法
 *
 * @author Administrator
 */
object JsonUtil {

    /**
     * 把一个map变成json字符串
     *
     * @param map
     * @return
     */
    @Volatile internal var gson: Gson? = null
    @JvmStatic
    fun parseMapToJson(map: Map<*, *>): String? {
        try {
            if (gson == null) {
                gson = Gson()
            }
            return gson!!.toJson(map)
        } catch (e: Exception) {
        }

        return null
    }
    @JvmStatic
    fun parseObjToJson(obj: Any): String? {
        try {
            if (gson == null) {
                gson = Gson()
            }
            return gson!!.toJson(obj)
        } catch (e: Exception) {
        }

        return null
    }

    /**
     * 把一个json字符串变成对象
     *
     * @param json
     * @param cls
     * @return
     */
    @JvmStatic
    fun <T> parseJsonToBean(json: String, cls: Class<T>): T? {
        if (gson == null) {
            gson = Gson()
        }
        var t: T? = null
        try {
            t = gson!!.fromJson(json, cls)
        } catch (e: Exception) {
            Log.e("parseJsonToBean", "parseJsonToBean: "+e.message )
        }

        return t
    }

    inline fun <reified Y > parseJsonToBean(json:String) : Y?{
        return parseJsonToBean(json,Y::class.java)
    }

    /**
     * 把json字符串变成map
     *
     * @param json
     * @return
     */
    @JvmStatic
    fun parseJsonToMap(json: String): HashMap<String, Any>? {
        val gson =  GsonBuilder().registerTypeAdapter(object :TypeToken<Map<String, Object>>(){}.getType(), ObjectTypeAdapterRewrite()).serializeNulls().create();

        val type = object : TypeToken<HashMap<String, Any>>() {

        }.type
        var map: HashMap<String, Any>? = null
        try {
            map = gson!!.fromJson<HashMap<String, Any>>(json, type)
        } catch (e: Exception) {
        }

        return map
    }

    /**
     * 把json字符串变成集合
     * params:  new TypeToken<List<yourbean>>(){}.getType()
     *
     * @param json
     * @param type  new TypeToken<List<yourbean>>(){}.getType()
     * @return
    </yourbean></yourbean> */
    @JvmStatic
    fun parseJsonToList(json: String, type: Type): List<*>? {
        val gson = Gson()
        return gson.fromJson<List<*>>(json, type)
    }

    /**
     * 获取json串中某个字段的值，注意，只能获取同一层级的value
     *
     * @param json
     * @param key
     * @return
     */
    @JvmStatic
    fun getFieldValue(json: String, key: String): String? {
        if (TextUtils.isEmpty(json))
            return null
        if (!json.contains(key))
            return ""
        var jsonObject: JSONObject? = null
        var value: String? = null
        try {
            jsonObject = JSONObject(json)
            value = jsonObject.getString(key)
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        return value
    }
    /**
     * 把String转成json,key为msg,value为String
     *
     * @param String
     * @param
     * @return
     */
    @JvmStatic
    fun parseStringToJsonWithMsg(input: String): String? {
        val map = mutableMapOf("msg" to input)
        return parseMapToJson(map)
    }

}
